﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Fin;
using ESOL.WinUI.MasterData;
using ESOL.Common;
using ESOL.WinUI.Pur;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageAPAccount : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstSupplierEntity mSelectedSupplier;
        private FinSupplierAccountEntity mSelectedSupplierAccount;
        #endregion

        #region Contructor
        public frmManageAPAccount()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (mSelectedSupplier != null)
            {
                condition += " AND SA.SUPPLIER_ID = '" + mSelectedSupplier.SupplierId + "' ";
            }
            if (dtpFromDate.EditValue!=null)
            {
                condition += " AND SA.AMOUNT_DATE >= CONVERT(DATETIME,'" + dtpFromDate.EditValue.ToString() + "')";
            }
            if (dtpEndDate.EditValue != null)
            {
                condition += " AND SA.AMOUNT_DATE <= CONVERT(DATETIME,'" + dtpEndDate.EditValue.ToString() + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = "";
            string orderBy = "S.SUPPLIER_NAME DESC";
            DataTable dt = FinSupplierAccountItemBs.GetTotalByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            if (string.IsNullOrEmpty(dr["SUPPLIER_ID"].ToString()) == false)
            {
                mSelectedSupplierAccount = FinSupplierAccountBs.GetBySupplierId(dr["SUPPLIER_ID"].ToString().Trim());
            }
            else
            {
                mSelectedSupplierAccount = null;
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedSupplierAccount == null)
            {
                MessageBox.Show("请选择一个供应商支出总账！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPAccountDetail frm = new frmMaintainAPAccountDetail(mSelectedSupplierAccount);
            frm.ShowDialog();
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                mSelectedSupplier = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                mSelectedSupplier = null;
            }
        }
        #endregion

    }
}
